import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import _ from 'lodash'
const baseUrlScheme = CommonConfig.schemaUrl + '/v1'
const baseUrlMachineStatus = CommonConfig.machinestatusUrl + '/v1'
const baseUrlMachineDetails = CommonConfig.machinestatusUrl + '/v2'
const datacenterUrl = CommonConfig.datacenterUrl + '/v1'
const tpmUrl = CommonConfig.tpmUrl + '/v1'
// const upmsUrl = CommonConfig.upmsUrl + '/v1'
// const baseUrlTpm = CommonConfig.url3

import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

// 获取机台详情
export function getMachineDetails (id) {
  let reqData = {
    params: {
      deviceId: id
    }
  }
  const newUrl = baseUrlMachineDetails + '/machine_status_details'

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取生产组织架构-树形选择器结构
export function getOrganize () {
  const newUrl = baseUrlScheme + '/scheme_tree'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 通过所在位置信息获取设备
export function getDevices (level, schemeId) {
  const newUrl = `${CommonConfig.tpmUrl}/v1/devices_scheme`

  let reqData = {
    params: {
      level,
      schemeId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取所有设备
 */
export function getAllDevice () {
  const newUrl = `${CommonConfig.tpmUrl}/v1/devices/all_device_with_schema`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取机台状态
export function getMachineStatusList (params, workshopId) {
  const newUrl = baseUrlMachineStatus + `/all_machine_status`
  return axiosInstance.post(newUrl, params, { params: { workshopId } }).then((res) => {
    return Promise.resolve(res)
  })
}

export function getTodayProductivity (workshopId, deviceId) {
  const newUrl = baseUrlMachineStatus + `/device/today_productivity?workshopId=${workshopId}&deviceId=${deviceId}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取机台详情(开动率、报警记录、停机时长)
export function getMachineStatusDetails (boxId, deviceId) {
  const newUrl = baseUrlMachineStatus + '/machine_status_details'

  const reqData = {
    params: {
      boxId: boxId,
      deviceId: deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
export function getMachineYDetails (deviceId) {
  const newUrl = baseUrlMachineStatus + '/device/threshold_alarm'

  const reqData = {
    params: {
      deviceId: deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取停机记录
export function getDowntimeDetails (boxId) {
  const newUrl = baseUrlMachineStatus + '/stop_cause_details'

  const reqData = {
    params: {
      boxId: boxId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取单次循环零件加工数量
export function getProcessCount (boxId, deviceId) {
  const newUrl = `${CommonConfig.machinestatusUrl}/v1/machine_status/process_count`

  const reqData = {
    params: {
      boxId: boxId,
      deviceId: deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取应完成数量
export function getMachineExpectedCount (boxId, deviceId) {
  const newUrl = baseUrlMachineStatus + '/machine_expected_count'

  const reqData = {
    params: {
      boxId: boxId,
      deviceId: deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取用时和开动率
export function getMachineTimeRate (boxId, deviceId) {
  const newUrl = baseUrlMachineStatus + '/machine_time_rate'

  const reqData = {
    params: {
      boxId: boxId,
      deviceId: deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取开动率图表数据
export function getMachineRateChart (boxId, deviceId) {
  const newUrl = baseUrlMachineStatus + '/machine_rate_chart'

  const reqData = {
    params: {
      boxId: boxId,
      deviceId: deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取正常循环列表
export function getNormalWorkpieces (id, since, before) {
  const newUrl = `${baseUrlMachineStatus}/devices/${id}/normal_workpieces`

  const reqData = {
    params: {
      start: since,
      end: before
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 根据盒子ID 列举状态持续时间明细
export function getDowntimeDuration (id, since, before, code) {
  const newUrl = `${baseUrlMachineStatus}/devices/${id}/device_status_durations`

  const reqData = {
    params: {
      start: since,
      end: before,
      code
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 根据盒子ID 列举状态持续时间明细
export function getDowntimeList ({ deviceId, since, end, stopDurationStart, stopDurationEnd, stopTypeIds, stopPerson, pageNum, pageSize }) {
  const newUrl = `${baseUrlMachineStatus}/devices/${deviceId}/stop_causes`

  const reqData = {
    params: {
      since,
      end,
      stopDurationStart,
      stopDurationEnd,
      stopTypeIds,
      pageNum,
      pageSize,
      operatorIds: _.isNil(stopPerson) ? [] : [stopPerson]
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
export function getUserName (name) {
  const newUrl = `${datacenterUrl}/users:nameFuzzy`

  const reqData = {
    params: {
      name
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 编辑停机明细
export function editDowntimeDuration (id, params) {
  const newUrl = `${baseUrlMachineStatus}/devices/${id}/device_status_durations`

  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 查询所有原因组
 */
export function getCauseTypeList (causeType) {
  const reqData = {
    params: {
      causeType,
      isFilter: false
    }
  }
  const url = causeType === 3 ? `${baseUrlMachineStatus}/stop_cause_type:selectBatch` : `${CommonConfig.upmsUrl}/v1/cause_type`
  return axiosInstance.get(url, reqData).then((res) => {
    return Promise.resolve((res))
  })
}

// 根据盒子ID 列举报警明细
export function getDeviceAlarms (id, since, before) {
  const newUrl = `${baseUrlMachineStatus}/devices/${id}/device_alarms`

  const reqData = {
    params: {
      start: since,
      end: before
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 根据盒子ID 列举异常报工明细
export function getAbnormalWorkpieces (id, since, before) {
  const newUrl = `${baseUrlMachineStatus}/devices/${id}/abnormal_workpieces`

  const reqData = {
    params: {
      start: since,
      end: before
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 异常循环标记正常
export function abnormalWorkpiecesRelease (timestamps) {
  const newUrl = `${baseUrlMachineStatus}/abnormal_workpieces:release`

  const params = {
    timestamps
  }
  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

// 查询当天班制的开始时间
export function getStartDateTime (boxId, deviceId, date) {
  const url = `${baseUrlMachineStatus}/machines_status/shifts/start_date_time`

  const reqData = {
    boxId,
    deviceId,
    date
  }

  return axiosInstance.get(url, { params: reqData }).then((res) => {
    return Promise.resolve(res)
  })
}

export function getMachineTimelineData (boxId, deviceId, date) {
  const url = `${baseUrlMachineStatus}/machine_time_line`

  const reqData = {
    boxId,
    deviceId,
    date
  }

  return axiosInstance.get(url, { params: reqData })
}

// 获取产品循环统计

export function getProductList (id, since, before) {
  const newUrl = `${baseUrlMachineStatus}/devices/${id}/normal_workpieces:circular`

  const reqData = {
    params: {
      start: since,
      end: before
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取产品循环统计

export function getDetailedCount (id, since, before) {
  const newUrl = `${baseUrlMachineStatus}/devices/${id}/timeline/detailed:count`

  const reqData = {
    params: {
      start: since,
      end: before
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 时间线报表
 * @param {*}
 */
export function getListTimeLine ({ boxIds, date, pageNum, pageSize, workshopId }) {
  const newUrl = `${baseUrlMachineStatus}/machine/list_time_line`

  const reqData = {
    params: {
      boxIds,
      date,
      pageNum,
      pageSize,
      workshopId
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// device/{id}/current/normal_workpieces
// 当日正常报工循环
export function getCurrentWorkpieces ({ id }) {
  const newUrl = `${baseUrlMachineStatus}/device/${id}/current/normal_workpieces`

  const reqData = {
    params: {
      id
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 获取开动率
export function getAvailability (id) {
  const newUrl = `${baseUrlMachineStatus}/devices/${id}/availability_rate`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
// 实时下行数据
export function sendMsg (deviceId) {
  const newUrl = `${baseUrlMachineStatus}/realtime_onlineprocess/send`
  const reqData = {
    params: {
      cmd: 1,
      deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 实时uat环境数据展示
// /v1/real_report/send
// 请求实时数据/在线加工V2
// 实时下行数据
export function sendUatMsg ({
  cmd,
  deviceId,
  path,
  reportType
}) {
  const newUrl = `${baseUrlMachineStatus}/real_report/send`
  const params = {
    cmd,
    deviceId,
    duration: 60,
    interval_time: 1,
    path,
    report_type: reportType
  }
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 检查设备是否和盒子解绑
 * @param {*} deviceId
 */
export function isUnBind (deviceId) {
  const newUrl = `tpm/v1/device/${deviceId}/bind_state:check`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 查询车间设备未设置摄像头的车间列表
 */
export function getDeviceDisCamera () {
  const newUrl = `${baseUrlScheme}/workshops/device_dis_carema`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 查询未设置车间摄像头的车间列表
 */
export function getDisCamera () {
  const newUrl = `${baseUrlScheme}/workshops/dis_carema`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据车间id查询产线
 */
export function getLinesByWorkshop (workShopId) {
  const newUrl = `${baseUrlScheme}/lines_by_workshop/filter_default`
  return axiosInstance.get(newUrl, { params: { workShopId } }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 查询配置摄像头的车间
 */
export function getCameraConfig () {
  const newUrl = `${tpmUrl}/workshop/camera_config`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 查询配置摄像头的设备的车间
 */
export function getDeviceCameraConfig () {
  const newUrl = `${tpmUrl}/workshop/device_camera_config`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据车间id查询车间设备配置的摄像头
 * @param workshopId
 */
export function getDeviceCameraConfigSet (workshopId) {
  const newUrl = `${tpmUrl}/workshop/${workshopId}/device/camera_config`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据车间id查询车间配置的摄像头
 * @param workshopId
 */
export function getCameraConfigSet (workshopId) {
  const newUrl = `${tpmUrl}/workshop/${workshopId}/camera_config`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 查询车间下未设置摄像头的设备
 * @param params
 */
export function getNotSetDeviceCamera (params) {
  const newUrl = `${tpmUrl}/workshop/${params.workshopId}/devices/dis_camera`
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 *新增/更新车间设备摄像头
 * @param data
 */
export function addDeviceCamera (data) {
  const newUrl = `${tpmUrl}/workshop/device/camera_config`
  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 新增车间摄像头
 * @param data
 */
export function addCamera (data) {
  const newUrl = `${tpmUrl}/workshop/camera_config`
  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据车间id删除车间设备摄像头
 * @param workshopId
 */
export function deleteDeviceCamera (workshopId) {
  const newUrl = `${tpmUrl}/workshop/${workshopId}/device/camera_config`
  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据车间id删除车间设备摄像头
 * @param workshopId
 */
export function deleteCamera (workshopId) {
  const newUrl = `${tpmUrl}/workshop/${workshopId}/camera_config`
  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据摄像头id删除摄像头
 * @param id
 */
export function deleteCameraById (id) {
  const newUrl = `${tpmUrl}/camera_config/${id}`
  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 查询当前未解除停机状态原因
export function getCurrentStopCause (deviceId) {
  const newUrl = `${baseUrlMachineStatus}/device/current_stop_cause/${deviceId}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 设置当前未解除停机状态原因
export function setCurrentStopCause (deviceId, data) {
  const newUrl = `${baseUrlMachineStatus}/device/current_stop_cause?deviceId=${deviceId}`
  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

// plc阈值设置列表
export function getPlcWarnSet (params) {
  const newUrl = `${baseUrlMachineStatus}/listPlcWarnSet_By_DeviceId`
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

// plc阈值key列表
export function getListPlcWarnKey (deviceId) {
  const newUrl = `${baseUrlMachineStatus}/listPlcWarnKey_By_DeviceId`
  return axiosInstance.get(newUrl, { params: { deviceId } }).then((res) => {
    return Promise.resolve(res)
  })
}

// plc阈值设置列表
export function deletePlcSet (plcWarnID) {
  const newUrl = `${baseUrlMachineStatus}/plcWarnSet/${plcWarnID}`
  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 修改plc阈值设置
export function setPlcWarnSet (data) {
  const newUrl = `${baseUrlMachineStatus}/plcWarnSet`
  return axiosInstance.put(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

export function exportPlcWarnExcel (data) {
  const newUrl = `${baseUrlMachineStatus}/plcWarnSet/export`
  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

// 查询机台plc实时数据
export function getPlcData (deviceId) {
  const newUrl = `${baseUrlMachineStatus}/machine_plc_realdata`
  return axiosInstance.get(newUrl, { params: { deviceId } }).then((res) => {
    return Promise.resolve(res)
  })
}
// 获取设备全部点位组和点位关联信息
export function getPointsGroup (deviceId) {
  // console.log(deviceId, 'deviceId')
  const newUrl = `${tpmUrl}/plc_group/groups/${deviceId}?all=false`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res.data)
  })
}
// 保存设备全部点位组和点位关联信息
export function savePointsGroup (deviceId, params) {
  // console.log(deviceId, 'deviceId')
  const newUrl = `${tpmUrl}/plc_group/groups/${deviceId}`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
// 查询采集设备实时数据点位
export function getPointsList (deviceId) {
  // console.log(deviceId, 'deviceId')
  const newUrl = `${tpmUrl}/plc_collect_point/realtime_points`
  return axiosInstance.get(newUrl, { params: { deviceId } }).then((res) => {
    return Promise.resolve(res)
  })
}

// 查询采集设备实时数据点位
export function getPoints (params) {
  const newUrl = `${tpmUrl}/device/realtime_points`
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

// 保存设备实时数据点位
export function savePoints (params) {
  const newUrl = `${tpmUrl}/plc_collect_point/realtime_points/save`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
// 查询所选数据点位历史数据
export function getPlcDataList (params) {
  const reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const newUrl = `${baseUrlMachineStatus}/device_plc_collect_data/list`
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
// 查询所选数据点位历史数据服（复选）
export function getCheckPlcDataList (params) {
  const newUrl = `${baseUrlMachineStatus}/device_plc_collect_data/list/filled`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res.data)
  })
}

// 导出所选数据点位历史数据
export function exportPlcData (params) {
  const reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const newUrl = `${baseUrlMachineStatus}/device_plc_collect_data/export`
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
